package com.ay.weixin.fdc.front.bank.event;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;

import org.lizar.config.keys.Keys;
import org.lizar.controller.Event;
import org.lizar.controller.loader.EventLoader;
import org.lizar.ioc.Container;
import org.lizar.util.StringHelper;

import com.ay.weixin.config.ProjectConfig;
import com.ay.weixin.fdc.admin.category.domain.CategoryLoader;
import com.ay.weixin.fdc.boss.bank.db.BankAccountDao;
import com.ay.weixin.fdc.boss.bank.domain.BankAccount;
import com.ay.weixin.fdc.boss.user.domain.UserBasic;

public class BankAccountEvt extends Event {

	private BankAccountDao bank_dao;
	
	private CategoryLoader cl;
	
	@Override
	public void init_property() throws Exception {
		// TODO Auto-generated method stub
		bank_dao=Container.get(BankAccountDao.class);
		cl=Container.get(CategoryLoader.class);
	}

	@Override
	public String default_handling_path() {
		// TODO Auto-generated method stub
		return "/bank/*";
	}

	@Override
	public void handle(EventLoader el) throws ServletException, IOException {
		long wxproject_id=el.queryPathInt(1);
		
		if(!el.getBool("is_weixin")&&ProjectConfig._bool(wxproject_id,"sys.weixin_only")){
			el.text("invalid visit");
			return;
		}
		if(el.getSessionAttr("user")==null){
			el.response_to_root("/fdc/login");
			return;
		}
		if(el.queryPathCheck(1,"")){
			view(el,wxproject_id);
		}else if(el.queryPathCheck(1,"binding")){//银行卡绑定
			bankbinding(el,wxproject_id);
		}else if(el.queryPathCheck(1,"update_binding")){//银行卡更新
			updatebinding(el,wxproject_id);
		}
		
		
	}
	
	private void view(EventLoader el,long wxproject_id) throws IOException, ServletException{
		UserBasic user=el.getSessionAttr("user",UserBasic.class);
		List<BankAccount> bank_list=null;
		
		try {
			bank_list=bank_dao.getByUserId(user.getId());
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg",  el.i18("bank.db.failure"));
		}
		
		el.setAttr("bank_list",bank_list);
		el.setAttr("bank_category",cl.get("bank"));
		el.template("/WEB-INF/template/app/"+wxproject_id+"/bank/binding/index.vm");
	}

	private void bankbinding(EventLoader el,long wxproject_id) throws IOException, ServletException{
		String name=el.getStr("name");
		String account=el.getStr("account");
		int bank_id=el.getInt("bank_id");
		String remark=el.getStr("remark");
		String msg=null;
		UserBasic user=el.getSessionAttr("user",UserBasic.class);
		if(StringHelper.isNull(name)||name.length()>Keys.getInt("bank.name_max_length")){
			msg=String.format(el.i18("bank.name.not.valid"), Keys.getInt("bank.name_max_length"));
			el.setAttr("msg",msg);
	        view(el,wxproject_id);
			return;
		}
		
		if(StringHelper.isNull(account)||account.length()>Keys.getInt("bank.account_max_length")){
			msg=String.format(el.i18("bank.account.not.valid"), Keys.getInt("bank.account_max_length"));
			el.setAttr("msg",msg);
	        view(el,wxproject_id);
			return;
		}
		
		if(remark.length()>Keys.getInt("bank.remark_max_length")){
			msg=String.format(el.i18("bank.remark.not.valid"), Keys.getInt("bank.remark_max_length"));
			el.setAttr("msg",msg);
	        view(el,wxproject_id);
			return;
		}
		
		if(cl.get(bank_id)==null){
			msg=String.format(el.i18("bank.not.valid"));
			el.setAttr("msg",msg);
	        view(el,wxproject_id);
			return;
		}
		
		List<BankAccount> blist=null;
		
		try {
			blist=bank_dao.getByUserId(user.getId());
		} catch (SQLException e1) {
			e1.printStackTrace();
			el.setAttr("msg", el.i18("bank.db.failure"));
			view(el,wxproject_id);
			return;
		}
		
		if(blist!=null&&blist.size()>0){
			el.setAttr("msg",el.i18("bank.banding.sucess"));
			view(el,wxproject_id);
			return;
		}
		
		BankAccount bank=new BankAccount();
		bank.setAccount(account);
		bank.setBank_id(bank_id);
		bank.setRemark(remark);
		bank.setName(name);
		bank.setStatus(Keys.getInt("bank.default_staus"));
		bank.setUser_id(user.getId());
		try {
			bank_dao.insert(bank);
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg", el.i18("bank.db.failure"));
			view(el,wxproject_id);
			return;
		}
		el.setAttr("msg",el.i18("bank.banding.sucess"));
		el.template("/WEB-INF/template/app/"+wxproject_id+"/bank/binding/result.vm");
	}
	
	private void updatebinding(EventLoader el,long wxproject_id) throws IOException, ServletException{
		String name=el.getStr("name");
		String account=el.getStr("account");
		int bank_id=el.getInt("bank_id");
		String remark=el.getStr("remark");
		long id=el.getLong("id");
		String msg=null;
		if(StringHelper.isNull(name)||name.length()>Keys.getInt("bank.name_max_length")){
			msg=String.format(el.i18("bank.name.not.valid"), Keys.getInt("bank.name_max_length"));
			el.setAttr("msg",msg);
	        view(el,wxproject_id);
			return;
		}
		
		if(StringHelper.isNull(account)||account.length()>Keys.getInt("bank.account_max_length")){
			msg=String.format(el.i18("bank.account.not.valid"), Keys.getInt("bank.account_max_length"));
			el.setAttr("msg",msg);
	        view(el,wxproject_id);
			return;
		}
		
		if(remark.length()>Keys.getInt("bank.remark_max_length")){
			msg=String.format(el.i18("bank.remark.not.valid"), Keys.getInt("bank.remark_max_length"));
			el.setAttr("msg",msg);
	        view(el,wxproject_id);
			return;
		}
		
		if(cl.get(bank_id)==null){
			msg=String.format(el.i18("bank.not.valid"));
			el.setAttr("msg",msg);
	        view(el,wxproject_id);
			return;
		}
		
		UserBasic user=el.getSessionAttr("user",UserBasic.class);
		BankAccount bank=new BankAccount();
		bank.setAccount(account);
		bank.setBank_id(bank_id);
		bank.setRemark(remark);
		bank.setName(name);
		bank.setUser_id(user.getId());
		bank.setId(id);
		try {
			bank_dao.update(bank);
		} catch (SQLException e) {
			e.printStackTrace();
			el.setAttr("msg", el.i18("bank.db.failure"));
			view(el,wxproject_id);
			return;
		}
		el.setAttr("msg",el.i18("bank.banding.sucess"));
		el.template("/WEB-INF/template/app/"+wxproject_id+"/bank/binding/result.vm");
	}
}
